package com.java.business.controller.admin;

import java.util.Arrays;
import java.util.List;
import javax.validation.Valid;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.java.business.entity.CourseContentFile;
import com.java.business.service.CourseContentFileService;
import com.java.common.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.java.common.utils.R;



/**
 * 课程内容文件
 *
 * @author jl
 * @since 2020-10-07 10:31:35
 */
@RestController
@RequestMapping("admin/course-content-file")
public class CourseContentFileController {
    @Autowired
    private CourseContentFileService courseContentFileService;

    /**
     * 带条件分页查询课程内容文件
     */
    @GetMapping("/list/{courseId}")
    public R list(@PathVariable("courseId")String courseId){
        List<CourseContentFile> list = courseContentFileService.list(new LambdaQueryWrapper<CourseContentFile>().eq(CourseContentFile::getCourseId, courseId));
        return R.ok(list);
    }


    /**
     * 根据主键查询课程内容文件
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
        CourseContentFile courseContentFile = courseContentFileService.getById(id);

        return R.ok(courseContentFile);
    }


    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public R save(@RequestBody CourseContentFile courseContentFile) {
        if (StrUtil.isNotBlank(courseContentFile.getId())) {
            courseContentFileService.updateById(courseContentFile);
        } else {
            courseContentFile.setId(UuidUtil.getShortUuid());
            courseContentFileService.save(courseContentFile);
        }
        return R.ok(courseContentFile);
    }

    /**
     * 删除课程内容文件
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable String id) {
        courseContentFileService.removeById(id);
        return R.ok();
    }

}
